package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.api.db.BlackListApi;
import com.tanhua.dubbo.api.mapper.BlackListMapper;
import com.tanhua.model.db.BlackList;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class BlackListApiImpl implements BlackListApi {

    @Autowired
    private BlackListMapper blackListMapper;

    /**
     * 分页查询黑名单列表数据
     * @param page
     * @param pagesize
     * @param userId
     * @return
     */
    @Override
    public PageResult<BlackList> blackListPage(Long page, Long pagesize, Long userId) {
        // 1、构造分页对象
        IPage<BlackList> queryResult = new Page<>(page,pagesize);

        // 2、构造分页查询条件
        QueryWrapper<BlackList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);

        // 3、执行分页查询
        IPage<BlackList> p = blackListMapper.selectPage(queryResult, queryWrapper);

        // 4、构造PageResult对象进行返回
        PageResult<BlackList> pageResult = new PageResult<>(p.getTotal(), p.getSize(), p.getPages(), page, p.getRecords());

        return pageResult;
    }

    /**
     * 黑名单 - 移除
     * @param uid
     * @param loginUserId
     */
    @Override
    public void removeBlackList(Long uid, Long loginUserId) {
        QueryWrapper<BlackList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("black_user_id",uid).eq("user_id",loginUserId);
        blackListMapper.delete(queryWrapper);
    }
}
